<?php
/**
 * Created by PhpStorm.
 * User: VJLau
 * Date: 2018/1/30
 * Time: 下午3:12
 */

load()->func('tpl');

$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($operation == 'display') {
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $list = pdo_fetchall("SELECT * FROM "
        . tablename('bontrip_quality') . " WHERE uniacid = '{$uniacid}' and deleted_at is null"
        . " ORDER BY id DESC LIMIT "
        . ($pindex - 1) * $psize . ',' . $psize);
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM "
        . tablename('bontrip_quality') . " WHERE uniacid = '{$uniacid}' and deleted_at is null");
    $pager = pagination($total, $pindex, $psize);

} elseif ($operation == 'post') {
    $id = intval($_GPC['id']);
    $quality = pdo_fetch("select * from " . tablename('bontrip_quality') . " where id={$id} and uniacid={$uniacid}");
    if (checksubmit('submit')) {
        $data = array(
            'uniacid' => $uniacid,
            'sort' => intval($_GPC['sort']),
            'tag' => str_replace('，',',',$_GPC['tag']),
            'label' => str_replace('，', ',', $_GPC['label']),
            'name' => $_GPC['name'],
            'thumb' => $_GPC['thumb'],
            'thumbs' => serialize($_GPC['thumbs']),
            'introduction' => $_GPC['introduction'],
            'remark' => $_GPC['remark'],
            'currency_id' => $_GPC['currency_id'],
            'adult_price' => $_GPC['adult_price'],
            'child_price' => $_GPC['child_price'],
            'baby_price' => $_GPC['baby_price'],
            'is_index' => intval($_GPC['is_index']),
            'updated_at' => TIMESTAMP
        );

        $product = $_GPC['product'];
        $price = $_GPC['price'];
        $use = $_GPC['use'];
        $buy = $_GPC['buy'];
        //重置索引
        if (!empty($product["trip"])) {
            $product['trip'] = array_values($product["trip"]);
        }
        if (!empty($price["price"])) {
            $price['price'] = array_values($price["price"]);
        }
        if (!empty($use["use"])) {
            $use['use'] = array_values($use["use"]);
        }
        if (!empty($buy["buy"])) {
            $buy['buy'] = array_values($buy["buy"]);
        }
        $data['product_desc'] = serialize($product);
        $data['price_desc'] = serialize($price);
        $data['use_desc'] = serialize($use);
        $data['buy_desc'] = serialize($buy);

        if (!empty($_GPC['sku'])) {
            $sku_ids = implode(',', array_keys($_GPC['sku']));
            $skus = pdo_fetchall("SELECT id,name FROM " . tablename('bontrip_sku')
                . " WHERE id in ({$sku_ids}) AND deleted_at is null");
            $data['sku'] = serialize($skus);
        } else {
            $data['sku'] = serialize(array());
        }

        if (!empty($id)) {
            pdo_update('bontrip_quality', $data, array('id' => $id));
        } else {
            $data['created_at'] = $data['updated_at'];
            pdo_insert('bontrip_quality', $data);
            $id = pdo_insertid();
        }

        //自减
        $qd = pdo_fetchall("SELECT dest_id FROM "
            . tablename('bontrip_quality_dest') . " WHERE quality_id = {$id}");
        if (!empty($qd)) {
            foreach ($qd as $val) {
                $dest = pdo_fetch("select * from " . tablename('bontrip_dest') . " where id={$val['dest_id']} and uniacid={$uniacid}");
                pdo_update('bontrip_dest', array('quality_number' => $dest['quality_number'] - 1), array('id' => $dest['id']));
            }
        }

        pdo_delete('bontrip_quality_dest', array('quality_id' => $id));
        $dests = $_GPC['dests'];
        if (!empty($dests)) {
            foreach ($dests as $dest) {
                $dest_data = array(
                    'quality_id' => $id,
                    'dest_id' => $dest
                );
                pdo_insert('bontrip_quality_dest', $dest_data);
                $dest = pdo_fetch("select * from " . tablename('bontrip_dest') . " where id={$dest} and uniacid={$uniacid}");
                pdo_update('bontrip_dest', array('quality_number' => $dest['quality_number'] + 1), array('id' => $dest['id']));
            }
        }


        message('保存精品游成功！', $this->createWebUrl('quality', array('op' => 'display')), 'success');
    }
    if (!empty($quality['thumbs'])) {
        $quality['thumbs'] = unserialize($quality['thumbs']);
    }
    if (!empty($quality['product_desc'])) {
        $quality['product_desc'] = unserialize($quality['product_desc']);
    }
    $trip_num = empty($quality["product_desc"]["trip"]) ? 0 : count($quality["product_desc"]["trip"]);

    if (!empty($quality['price_desc'])) {
        $quality['price_desc'] = unserialize($quality['price_desc']);
    }
    $price_num = empty($quality["price_desc"]["price"]) ? 0 : count($quality["price_desc"]["price"]);

    if (!empty($quality['use_desc'])) {
        $quality['use_desc'] = unserialize($quality['use_desc']);
    }
    $use_num = empty($quality["use_desc"]["use"]) ? 0 : count($quality["use_desc"]["use"]);

    if (!empty($quality['buy_desc'])) {
        $quality['buy_desc'] = unserialize($quality['buy_desc']);
    }
    $buy_num = empty($quality["buy_desc"]["buy"]) ? 0 : count($quality["buy_desc"]["buy"]);
    $skus = pdo_fetchall("SELECT * FROM " . tablename('bontrip_sku')
        . " WHERE deleted_at is null");

    $quality_dests = pdo_fetchall("SELECT dest_id FROM "
        . tablename('bontrip_quality_dest') . " WHERE quality_id = '{$id}'"
        . " ORDER BY id DESC");
    $quality_dest_array = array();
    if (!empty($quality_dests)) {
        foreach ($quality_dests as $quality_dest) {
            array_push($quality_dest_array, $quality_dest['dest_id']);
        }
    }
    $quality_sku_array = array();
    $quality['sku'] = unserialize($quality['sku']);
    if (!empty($quality['sku'])) {
        foreach ($quality['sku'] as $sku) {
            array_push($quality_sku_array, $sku['id']);
        }
    }
    $region = pdo_fetchall("SELECT * FROM " . tablename('bontrip_region')
        . " WHERE uniacid = '{$uniacid}' and deleted_at is null ORDER BY id DESC");
    $dests = array();
    if (!empty($region)) {
        foreach ($region as $key => $val) {
            $dests[$key]['region_name'] = $val['name'];
            $dests[$key]['dest'] = pdo_fetchall("SELECT * FROM " . tablename('bontrip_dest')
                . " WHERE uniacid = '{$uniacid}' and deleted_at is null and region_id = {$val['id']} ORDER BY id DESC");
        }
    }
    $currencys = pdo_fetchall("SELECT * FROM " . tablename('bontrip_currency')
        . " WHERE deleted_at is null ORDER BY id DESC");


} elseif ($operation == 'delete') {
    $id = intval($_GPC['id']);
    $quality = pdo_fetch("SELECT id  FROM " . tablename('bontrip_quality')
        . " WHERE id={$id} AND uniacid={$uniacid} AND deleted_at is null");
    if (empty($quality)) {
        message('抱歉，精品游不存在或是已经被删除！', $this->createWebUrl('quality', array('op' => 'display')), 'error');
    }
    pdo_update('bontrip_quality', array('deleted_at' => time()), array('id' => $id));
    $qd = pdo_fetchall("SELECT dest_id FROM "
        . tablename('bontrip_quality_dest') . " WHERE quality_id = {$id}");
    if (!empty($qd)) {
        foreach ($qd as $val) {
            $dest = pdo_fetch("select * from " . tablename('bontrip_dest') . " where id={$val['dest_id']} and uniacid={$uniacid}");
            pdo_update('bontrip_dest', array('quality_number' => $dest['quality_number'] - 1), array('id' => $dest['id']));
        }
    }
    message('精品游删除成功！', $this->createWebUrl('quality', array('op' => 'display')), 'success');

} else {
    message('请求方式不存在');

}
include $this->template('quality', TEMPLATE_INCLUDEPATH, true);